package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.AdminMenu;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.service.MenuService;
import com.javasm.service.impl.MenuServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @version 0.1
 * @className: MenuServlet
 * @description:
 * @author: gfs
 * @date: 2025/6/23 9:58
 * @since: jdk17
 */
@WebServlet("/menus/*")
public class MenuServlet extends BaseServlet {

    //菜单查询
    public void listMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

        //1接收参数 转格式 封装对象
        //页码参数
        //必填项 可以通过设置默认值 变成非必填项
        //非必填项 不要给默认值
        Integer page = 1;
        Integer pageSize = 10;

        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //如果传了 就用传来的值 没传使用默认值
        if(pageStr!=null&&!"".equals(pageStr)){
            page = Integer.valueOf(pageStr);
        }
        if(pageSizeStr!=null&&!"".equals(pageSizeStr)){
            pageSize = Integer.valueOf(pageSizeStr);
        }
        //查询条件
        String menuname = req.getParameter("menuname");
        String pidStr = req.getParameter("pid");

        Long pid = null;
        if(pidStr!=null&&!"".equals(pidStr)){
            pid = Long.valueOf(pidStr);
        }
        AdminMenu inputMenu = new AdminMenu(menuname,pid);
        //2调用service
        MenuService menuService = new MenuServiceImpl();
        //给分页组件使用
        Integer total = menuService.countMenuByCondition(inputMenu);
        //算出总记录数
        Integer totalPage = PageInfo.getTotalPage(total, pageSize);
        //如果page 超过 总页数  按总页数查
        if(page>1 && page>totalPage){
            page = totalPage;
        }


        //给table使用
        List<AdminMenu> listMenus = menuService.listMenuByCondition(inputMenu, page, pageSize);

        //3根据执行结果 反馈信息
        ReturnResult returnResult = new ReturnResult();
        if(listMenus.size()>0){
            //把页码相关参数 都放在一起 方便前端覆盖
            //page pageSize total
            PageInfo pageInfo = new PageInfo(page, pageSize, total);
            returnResult.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());
            returnResult.setPageInfo(pageInfo);
            returnResult.setReturnData(listMenus);
        }else{
            returnResult.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }


        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();


    };
    //下拉列表查询
    public void listSelect(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        MenuService menuService = new MenuServiceImpl();
        List<AdminMenu> listMenuLevel1 = menuService.listMenuLevel1();

        ReturnResult returnResult = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(),
                ReturnCodeEnum.QUERY_SUCCESS.getMsg(),listMenuLevel1);
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();


    };
    //添加菜单
    public void addMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接请求参数 封装兑现
        String midStr = req.getParameter("mid");
        String menuname = req.getParameter("menuname");
        String pidStr = req.getParameter("pid");
        String url = req.getParameter("url");
        String glyphicon = req.getParameter("glyphicon");

        Long mid = null;
        Long pid = null;
        if(midStr!=null&&!"".equals(midStr)){
            mid = Long.valueOf(midStr);
        }
        if(pidStr!=null&&!"".equals(pidStr)){
            pid = Long.valueOf(pidStr);
        }
        AdminMenu inputMenu = new AdminMenu(mid, menuname, pid, url, glyphicon);
        //2调用sercie
        MenuService menuService = new MenuServiceImpl();
        Integer resNum = menuService.insertMenu(inputMenu);
        //3反馈响应数据
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }

    //查询菜单可用图标
    public void listIcon(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuService menuService = new MenuServiceImpl();
        List<String> listIcon = menuService.listMenuIcon();
        ReturnResult returnResult = new ReturnResult(ReturnCodeEnum.QUERY_SUCCESS.getCode(), ReturnCodeEnum.QUERY_SUCCESS.getMsg(), listIcon);
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }

    //查询修改的菜单信息
    public void getMenuById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String midStr = req.getParameter("mid");

        Long mid = null;
        if(midStr!=null&&!"".equals(midStr)){
            mid = Long.valueOf(midStr);
        }

        MenuServiceImpl menuService = new MenuServiceImpl();
        AdminMenu menuById = menuService.getById(mid);

        ReturnResult returnResult = new ReturnResult();
        if(menuById!=null){
            returnResult.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());
            //直接返回对象
            returnResult.setReturnData(menuById);
        }else{
            returnResult.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            returnResult.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }

        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }
    //修改菜单信息
    public void updateMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接请求参数 封装兑现
        String midStr = req.getParameter("mid");
        String menuname = req.getParameter("menuname");
        String pidStr = req.getParameter("pid");
        String url = req.getParameter("url");
        String glyphicon = req.getParameter("glyphicon");

        Long mid = null;
        Long pid = null;
        if(midStr!=null&&!"".equals(midStr)){
            mid = Long.valueOf(midStr);
        }
        if(pidStr!=null&&!"".equals(pidStr)){
            pid = Long.valueOf(pidStr);
        }
        AdminMenu inputMenu = new AdminMenu(mid, menuname, pid, url, glyphicon);

        MenuServiceImpl menuService = new MenuServiceImpl();
        Integer resNum = menuService.updateMenu(inputMenu);

        //3反馈响应数据
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();

    }

    //单条删除菜单
    public void deleteMenu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String midStr = req.getParameter("mid");

        Long mid = null;
        if(midStr!=null&&!"".equals(midStr)){
            mid = Long.valueOf(midStr);
        }
        MenuServiceImpl menuService = new MenuServiceImpl();
        Integer resNum = menuService.deleteMenu(mid);

        //3反馈响应数据
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();
    }

    //多条删除菜单
    public void deleteMenuMultiple(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //11,22,33
        String listMidStr = req.getParameter("listMid");
        ArrayList<Long> listMid = new ArrayList<>();
        //字符串拆成数组 转成Long 加入listMid
        for(String mid : listMidStr.split(",")){
            listMid.add(Long.valueOf(mid));
        }
        MenuServiceImpl menuService = new MenuServiceImpl();
        Integer resNum = menuService.deleteMenuMultiple(listMid);

        //3反馈响应数据
        ReturnResult returnResult = new ReturnResult();
        if(resNum>0){
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());
        }else{
            returnResult.setCode(ReturnCodeEnum.DATA_OPERATION_FAILED.getCode());
            returnResult.setMsg(ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg());
        }
        //用输出流输出JSON字符串
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(returnResult));
        writer.close();


    }


    }
